library NightVisionGoggles initializer init requires LegacySystem


	globals
		private boolean NVGEnabled = true
	endglobals
	
	function SetNVGEnabled takes boolean b returns nothing
		set NVGEnabled = b
	endfunction
	
	private function NVGConditions takes nothing returns boolean
		return GetSpellAbilityId()=='A04R' and NVGEnabled
	endfunction

	private function NVG takes nothing returns nothing
		local unit u = GetTriggerUnit()
		local player p = GetOwningPlayer(u)
		local integer i = GetConvertedPlayerId(p)
		local integer id = GetUnitTypeId(u)
		set udg_NightVision[i] = not(udg_NightVision[i])
		call NightVision(p,udg_NightVision[i])
		set bj_meleeNearestMineDist = GetUnitX(u)
		set bj_lastTransmissionDuration = GetUnitY(u)
		if udg_NightVision[i] then
			if GetWidgetLife(udg_CommArray)<=0.405 then
				call UnitShareVision(udg_GoodSheep,p,true)
			endif
			if id=='Hpal' or id=='H000' then
				call UnitRemoveAbility(u,'A084')
			endif
			set udg_soundpath = "war3mapImported\\nvg_on2.wav"
			call ShiftMoveSpeed(u,3.)
			call ModifyHeroStat(bj_HEROSTAT_AGI,udg_Hero[i],bj_MODIFYMETHOD_ADD,2)
		else
			if GetWidgetLife(udg_CommArray)<=0.405 then
				call UnitShareVision(udg_GoodSheep,p,false)
			endif
			if id=='Hpal' or id=='H000' then
				call UnitAddAbility(u,'A084')
			endif
			set udg_soundpath = "war3mapImported\\nvg_off3.wav"
			call ShiftMoveSpeed(u,-3.)
			call ModifyHeroStat(bj_HEROSTAT_AGI,udg_Hero[i],bj_MODIFYMETHOD_SUB,2)
		endif
		call PlaySoundPath3D.execute()
		set u = null
	endfunction

	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_SPELL_EFFECT  )
		call TriggerAddCondition(t,Condition(function NVGConditions))
		call TriggerAddAction(t,function NVG)
	endfunction




endlibrary